﻿Public Class Profile
    Inherits System.Web.UI.Page

    Public ConnectionString As String = ConfigurationManager.AppSettings("ConnectionString").ToString
    Public objData As New DatabaseClass(ConnectionString)
    Public objDataRead As New DatabaseClass(ConnectionString)
    Public objFunction As New FunctionClass
    Public objAlert As New AlertClass
    Public objConfig As New Configs
    Dim Sql As String = ""

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            loadData()
        End If
    End Sub

    ' Kiem tra du lieu va load data cua khach hang
    Public Sub loadData()
        Try
            ' Kiem tra xem dang nhap chua, neu khong co redirect ve trang chu
            If (Session("CusUserName") <> Nothing) Or (Session("CusUserName") <> "") Or (Session("CusFullname") <> Nothing) Or (Session("CusFullname") <> "") Then
                txtUsername.Text = Session("CusUserName")
                txtName.Text = Session("CusFullname")
                txtEmail.Text = Session("CusEmail")
                txtPhone.Text = Session("CusPhone")
                txtAddress.Text = Session("CusAddress")
                txtIDCard.Text = Session("CusIdCard")
            Else
                Response.Redirect("\")
            End If
        Catch ex As Exception
        End Try
    End Sub

    ' xu ly su kien update thong tin
    Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Try
            ' Kiem tra xem co muon thay doi mat khau hay khong
            If chkPass.Checked Then
                Sql = "spo_CusChangePass "
                Sql &= objFunction.ValueCheck(Integer.Parse(Session("CusID").ToString)) & ","
                Sql &= objFunction.FieldCheck(txtNowPass.Text) & ","
                Sql &= objFunction.FieldCheck(txtPassword.Text)
                objData.ExeReader(Sql)
                If objData.dataRead.Read Then
                    If Integer.Parse(objData.dataRead("CusID").ToString()) = 0 Then
                        objAlert.ErrorBox(Page, "Mật khẩu hiện tại của bạn không đúng, hãy thử lại.")
                        Exit Sub
                    End If
                End If
            End If
            Sql = "spo_CusProfile "
            Sql &= objFunction.ValueCheck(Integer.Parse(Session("CusID").ToString)) & ","
            Sql &= objFunction.FieldCheck(txtEmail.Text) & ","
            Sql &= objFunction.FieldCheck(txtPhone.Text) & ","
            Sql &= objFunction.FieldUniCk(txtName.Text) & ","
            Sql &= objFunction.FieldUniCk(txtAddress.Text) & ","
            Sql &= objFunction.FieldCheck(txtIDCard.Text)
            objDataRead.ExeNonQuery(Sql)
            ' Gan lai thong tin mpoi vao Session
            Session("CusEmail") = txtEmail.Text
            Session("CusPhone") = txtPhone.Text
            Session("CusFullname") = txtName.Text
            Session("CusAddress") = txtAddress.Text
            Session("CusIdCard") = txtIDCard.Text
            objAlert.InfoBox(Page, "Bạn đã thay đổi thông tin cá nhân thành công", Request.Url.PathAndQuery)
        Catch ex As Exception
            If objConfig.Developer.Equals("1") Then
                objAlert.ErrorBox(Page, "File: " & (New System.Diagnostics.StackTrace).GetFrame(0).GetMethod.DeclaringType.Name & _
                                  " --- Function: " & (New System.Diagnostics.StackTrace).GetFrame(0).GetMethod.Name & _
                                  " --- Mess: " & ex.Message.Replace("'", """"))
            Else
                objAlert.ErrorBox(Page, "Lỗi kỹ thuật, hãy thông báo lại với Admin, xin cảm ơn.")
            End If
        End Try
    End Sub

End Class